package com.tianchi.lexiang.background;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.PwdSettingUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PwdSetting extends Activity implements OnClickListener {

	private PwdSettingUtil instence = PwdSettingUtil.getInstence();

	private EditText pwdNum;
	private TextView promptTxt;
	private Button sureBtn;
	private Button cancelBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pwd_setting);
		initView();
	}

	private void initView() {

		pwdNum = (EditText) findViewById(R.id.pwd_num);

		promptTxt = (TextView) findViewById(R.id.prompt_txt);

		sureBtn = (Button) findViewById(R.id.sure_btn);
		sureBtn.setOnClickListener(this);
		cancelBtn = (Button) findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.sure_btn:
			if (instence.changePassword(pwdNum.getText().toString())) {
				AlertDialog.Builder builder = new Builder(PwdSetting.this);
				builder.setIcon(android.R.drawable.ic_menu_info_details);
				builder.setTitle(getString(R.string.tip));
				builder.setMessage(getString(R.string.pwd_update_success));
				builder.setPositiveButton(getString(R.string.confirm),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								PwdSetting.this.finish();
							}
						});
				builder.create().show();
			} else {
				promptTxt.setText(R.string.error_prompt);
			}
			break;

		case R.id.cancel_btn:
			PwdSetting.this.finish();
			break;

		default:
			break;
		}
	}

}
